<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_province extends _init_model
	{
		public $PROVINCE_ID;
		public $PROVINCE_CODE;
		public $PROVINCE_NAME;
		public $GEO_ID;
		
		function mod_province(){
			parent::__construct();
			$this->tableName = "province";
		}
		
		function get_province_list($display_show_all=true){
			$this->db->select(array('PROVINCE_ID', 'PROVINCE_NAME'));
			$this->db->order_by('PROVINCE_NAME', 'ASC');
			$this->db->order_by('PROVINCE_CODE', 'ASC');
			$query = $this->db->get($this->tableName);
			
			$return = array();
			if ($query->num_rows() > 0)
			{
				if ($display_show_all)
					array_push($return, array('PROVINCE_ID' => -1,
												'PROVINCE_NAME' => "== ทั้งหมด =="));
				
				foreach ($query->result() as $row)
					array_push($return, array('PROVINCE_ID' => $row->PROVINCE_ID,
												'PROVINCE_NAME' => $row->PROVINCE_NAME));														
			}
			
			return $return;
		}
		function get_province_for_dropdownlist($display_show_all=true){
			$result = $this->get_province_list($display_show_all);
			
			$return = array();
			if (count($result) > 0){
				foreach ($result as $row)
					$return[$row['PROVINCE_ID']] = $row['PROVINCE_NAME']; 
			}
			
			return $return;
		}
		
		function get_province_list_by_geography($cafe_id=null)
		{
			$north_geo = array();
			$central_geo = array();
			$esan_geo = array();
			$east_geo = array();
			$west_geo = array();
			$south_geo = array();			
			
			$sqlcmd = 'SELECT PROVINCE_ID, PROVINCE_NAME, GEO_ID';
			$sqlcmd .= ' FROM ' . $this->tableName;			
			$sqlcmd .= ' ORDER BY PROVINCE_CODE ASC';							
			$query = $this->db->query($sqlcmd);			
			
			if ($query->num_rows() > 0)
			{		
				$p_array = array();		
				if (!is_null($cafe_id))
				{
					$this->load->model('local/data/cafe/mod_cafe_province_list');
					$p_result = $this->mod_cafe_province_list->get(array('CAF_ID' => $cafe_id), array('PROVINCE_LIST'));					
					$p_array = explode(',', $p_result[0]->PROVINCE_LIST);
				}
					
				foreach ($query->result() as $row)
				{
					$set = false;
					
					if (in_array($row->PROVINCE_ID, $p_array))
						$set = true;
						
					switch ($row->GEO_ID)
					{
						case '1': case 1:
							array_push($north_geo, array('PROVINCE_ID' => $row->PROVINCE_ID,
														'PROVINCE_NAME' => $row->PROVINCE_NAME,
														'SET' => $set));
							break;
						case '2': case 2:
							array_push($central_geo, array('PROVINCE_ID' => $row->PROVINCE_ID,
														'PROVINCE_NAME' => $row->PROVINCE_NAME,
														'SET' => $set));
							break;
						case '3': case 3:
							array_push($esan_geo, array('PROVINCE_ID' => $row->PROVINCE_ID,
														'PROVINCE_NAME' => $row->PROVINCE_NAME,
														'SET' => $set));
							break;
						case '4': case 4:
							array_push($east_geo, array('PROVINCE_ID' => $row->PROVINCE_ID,
														'PROVINCE_NAME' => $row->PROVINCE_NAME,
														'SET' => $set));
							break;
						case '5': case 5:
							array_push($west_geo, array('PROVINCE_ID' => $row->PROVINCE_ID,
														'PROVINCE_NAME' => $row->PROVINCE_NAME,
														'SET' => $set));
							break;
						case '6': case 6:
							array_push($south_geo, array('PROVINCE_ID' => $row->PROVINCE_ID,
														'PROVINCE_NAME' => $row->PROVINCE_NAME,
														'SET' => $set));
							break;
					}				
				}
			}
			
			$return = array('CENTRAL_GEO' => $central_geo,
							'NORTH_GEO' => $north_geo,
							'ESAN_GEO' => $esan_geo,
							'EAST_GEO' => $east_geo,
							'WEST_GEO' => $west_geo,
							'SOUTH_GEO' => $south_geo);		
			
			return $return;
		}
	}
?>